package co.yixiang.controller.common;

import co.yixiang.common.annotation.Limit;
import co.yixiang.common.enums.ResultCode;
import co.yixiang.common.enums.VerificationEnums;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.system.entity.domain.SmsSign;
import co.yixiang.modules.system.entity.domain.SmsTemplate;
import co.yixiang.modules.system.service.SmsSignService;
import co.yixiang.modules.system.service.SmsTemplateService;
import co.yixiang.modules.system.service.SmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 短信验证码接口
 *
 * @author Chopper
 * @since 2020/11/26 15:41
 */
@RestController
@Api(tags = "短信验证码接口")
@RequestMapping("/common/common/sms")
public class SmsController {

    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private SmsSignService smsSignService;
    @Autowired
    private SmsTemplateService smsTemplateService;


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "String", name = "mobile", value = "手机号")
    })
    @GetMapping("/{verificationEnums}/{mobile}")
    @ApiOperation(value = "发送短信验证码,一分钟同一个ip请求1次")
    public ResultMessage getSmsCode(@PathVariable String mobile, @PathVariable VerificationEnums verificationEnums) {
        smsUtil.sendSmsCode(mobile, VerificationEnums.BIN, mobile);
        return ResultUtil.success(ResultCode.VERIFICATION_SEND_SUCCESS);
    }

    @ApiOperation(value = "新增短信签名")
    @PostMapping
    public ResultMessage<SmsSign> save(@Valid SmsSign smsSign) {
        smsSignService.addSmsSign(smsSign);
        return ResultUtil.success();
    }



    @ApiOperation(value = "新增短信模板")
    @PostMapping(value = "/template")
    public ResultMessage<SmsTemplate> save(@Valid SmsTemplate smsTemplate) {
        smsTemplateService.addSmsTemplate(smsTemplate);
        return ResultUtil.success();
    }
}
